package com.itheima.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import com.itheima.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {
   /* @Override
    public List<User> findAll() {

        //获取Sqlsession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取mapper的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用代理对象的方法
        return mapper.findAll();
    }*/


    @Override
    public PageInfo<User> findAll(int pageNum, int pageSize) {

        //获取Sqlsession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取mapper的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);

        List<User> users=mapper.findAll();

        //设置分页参数对象
        PageInfo<User> pageInfo = new PageInfo<>(users);

        return pageInfo;


    }

    @Override
    public User login(User u) {
        //获取Sqlsession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取mapper的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用代理对象的方法
        User user=mapper.login(u);
        return user;
    }

    @Override
    public void add(User u) {
        //获取Sqlsession对象
        SqlSession sqlSession= MyBatisUtils.getSqlSession();
        //获取mapper的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用代理对象的方法
        mapper.add(u);
    }

    @Override
    public void delete(String delid) {
        //获取Sqlsession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取mapper的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用代理对象的方法
        mapper.delete(delid);
    }

    @Override
    public User findById(String upid) {
        //获取Sqlsession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取mapper的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用代理对象的方法
      User user = mapper.findById(upid);
        return user;
    }

    @Override
    public void update(User user) {
        //获取Sqlsession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        //获取mapper的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //调用代理对象的方法
        mapper.update(user);
    }


}
